<?php
//Test
class Application_Model_DbTable_Utilisateur extends Zend_Db_Table_Abstract
{
	protected $_name = 'utilisateur';
	
	function addUser($id_resp, $login, $pwd, $fname, $lname, $email, $role)
	{
		$data = array (
		"id_resp"=>$id_resp,
		"login"=>$login ,
		"pwd"=>$pwd ,
		"fname"=>$fname ,
		"lname"=>$lname ,		
		"email"=>$email ,
		"role"=>$role
		);
	
		$this->insert($data); 	
	}
	
	function updateUser($id, $id_resp, $login, $pwd, $fname, $lname, $email, $role)
	{
		$data = array (
		"id_resp"=>$id_resp,
		"login"=>$login ,
		"pwd"=>$pwd ,
		"fname"=>$fname ,
		"lname"=>$lname ,		
		"email"=>$email ,
		"role"=>$role
		);
		$this->update($data, 'id='.(int)$id);
	}
	
	function deleteUser($id)
	{
		$this->delete('id='.(int)$id);	
	}
	
	public function getUserByLogin($login)
	{		
		$result=$this->select()->where("login=?",$login);
		$row = $this->fetchRow($result);
		
		if(!$row)
		{
			throw new Exception("Could not find row $login");
		}
	return $row->toArray();
	}
	
	public function getUser($id)
	{
		$id=(int) $id;
		$row=$this->fetchRow('id='.$id);
		
		if(!$row)
		{
			throw new Exception("Could not find row $id");
		}
	return $row->toArray();
	}
	

	function getUsers()
	{
		return $this->fetchAll();		
	}
	
	function isLogin($login)
	{
		$result=$this->select()->where("login=?",$login);
		$row = $this->fetchRow($result);
		if ($row) 
			return true;
		else
			return false;
		
	}
	
	function isUser($login,$password)
	{
		$result=$this->select()->where("login=?",$login)->where("pwd=?",$password);
		$row = $this->fetchRow($result);
		if ($row) 
			return true;
		else
			return false;
	}
	
function isGroupe($login,$password,$role)
	{
		$result=$this->select()->where("login=?",$login)->where("pwd=?",$password)->where("role=?",$role);
		$row = $this->fetchRow($result);
		if ($row) 
			return true;
		else
			return false;
	}

	
function GroupeResponsable()
	{
		$result=$this->select()->where("role = ?", "Responsable");
		
			return $this->fetchAll($result) ;
		
			
	}
	
function GroupeCollaborateur()
	{
		$result=$this->select()->where("role = ?", "Collaborateur");
		
			return $this->fetchAll($result) ;
		
			
	}
function whichGroupe($login,$password)
	{
		$result=$this->select()->where("login=?",$login)->where("pwd=?",$password);
		$row = $this->fetchRow($result);
		if ($row) 
		{
			$data = $row->toArray();
			return $data["role"];
		print_r($data);
    	die();
		}
		else 
			return "";
	}
}